package cn.nfwx.tv.manager;

import android.content.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nfwx.tv.callback.ResponseListener;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.db.AppDatabase;
import cn.nfwx.tv.db.AppointDao;
import cn.nfwx.tv.events.Event;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.helper.UmengPushHelper;
import cn.nfwx.tv.okhttp.CustomOkHttpClient;
import cn.nfwx.tv.okhttp.UrlHelper;

/**
 * Created by zb.liu on 2017/11/20.
 */
public class AppointmentManager {
    private static String list_appiontment_url = Constants.test_url + "/nfwx-user/v1/appointment?";
    private static String make_appiontment_url = Constants.test_url + "/nfwx-user/v1/appointment?";
    private static String cancle_appiontment_url = Constants.test_url + "/nfwx-user/v1/appointment?";


    /***
     * 取消预约
     * @param context
     * @param callBack
     */
    public static void cancelAppointment(Context context, String json, String header, ResponseListener<String> callBack) {
        CustomOkHttpClient.deleteObjectRequestWithBody(context, cancle_appiontment_url, json, "cancelAppointment", header, callBack);
        Event.postAppiontmentEvent();
    }

    public static void makeAppointment(Context context, String channelId, String channelName, String programName, String time, String header, ResponseListener<String> callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("lid", channelId);
        params.put("liveName", channelName);
        params.put("name", programName);
        params.put("time", time);
        CustomOkHttpClient.postObjectRequestWithHeader(context, make_appiontment_url, params, Constants.TIMEOUT, "makeAppointment", header, callBack);
        // 预约更改
        Event.postAppiontmentEvent();
    }

    /**
     * 预约列表
     *
     * @param context
     * @param responseListener
     * @return
     */
    public static String requestAppointmentList(Context context, ResponseListener responseListener) {
        String request_tag = "requestAppointmentList";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkAppointUrl(UmengPushHelper.getInstance().getDeviceToken());
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }

    /**
     * 添加预约
     *
     * @param context
     * @param gid
     * @param program_id
     * @param responseListener
     * @return
     */
    public static String requestAddAppointment(Context context, String gid, ResponseListener responseListener, int... program_id) {
        String request_tag = "requestAddAppointment";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < program_id.length; i++) {
            sb.append(program_id[i] + ",");
        }
        String newStr = sb.toString();
        String str3 = newStr.substring(0, newStr.length() - 1);
        String url = UrlHelper.getIjkAddDelAppintUrl(UmengPushHelper.getInstance().getDeviceToken(), gid, str3, UrlHelper.IJK_ADD);
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }

    /**
     * 取消预约
     *
     * @param context
     * @param gid
     * @param responseListener
     * @param program_ids
     * @return
     */
    public static String requestDelAppointment(Context context, String gid, ResponseListener responseListener, List<Integer> program_ids) {
        String request_tag = "requestDelAppointment";
        AppointDao appointDao = AppDatabase.getInstance(context).appointDao();
        List<cn.nfwx.tv.db.TvAppointment> tvAppointments = appointDao.queryAllById(program_ids);
        cn.nfwx.tv.db.TvAppointment[] appointments = new cn.nfwx.tv.db.TvAppointment[tvAppointments.size()];
        tvAppointments.toArray(appointments);
        appointDao.delete(appointments);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < program_ids.size(); i++) {
            sb.append(program_ids.get(i) + ",");
        }
        String newStr = sb.toString();
        String str3 = newStr.substring(0, newStr.length() - 1);
        String url = UrlHelper.getIjkAddDelAppintUrl(UmengPushHelper.getInstance().getDeviceToken(), gid, str3, UrlHelper.IJK_DEL);
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }
}
